/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.animepersistance.dao.impl;

import desu.nya.animepersistance.dao.GenreDao;
import desu.nya.animepersistance.model.Genre;
import desu.nya.animepersistance.utils.AnimeHibDaoSupport;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 *
 * @author ievstratov
 */
@Repository("GenreDAO")
public class GenreDaoImpl extends AnimeHibDaoSupport implements GenreDao {

    @Override
    public List<Genre> findGenreByMask(Long mask) {
        List<Genre> glist;
        List<Long> genreListId = new ArrayList<Long>();
        int count = 0;
        for(int i=2; i<32; i++) {
            if((mask % (i*2)) >= (i-1)*2) {
                genreListId.add(new Long(i));
            }
        }
        if(count != 0)
        {
            Criteria cr = getSession().createCriteria(Genre.class);
            glist = cr.add(Expression.in("id", genreListId)).list();
        }
        else {
            glist = new ArrayList<Genre>();
        }
        return glist;
    }

    @Override
    public List<Genre> findAll() {
        List<Genre> list = getHibernateTemplate().find("from Genre");
        return list;
    }
    
}
